package com.weizhu.swufer.core.rpc;

import com.weizhu.swufer.constant.SwuferConstant;

public class RpcResponse {

    private int code;

    private String msg;

    private Object data;

    private Throwable error;

    public RpcResponse(){

    }

    public RpcResponse(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public void error(String errorMsg){
        this.code = SwuferConstant.RESPONSE_ERROR_CODE;
        this.msg = "trace-id:" + RpcContext.getContext().getParameter(SwuferConstant.TRACE_ID) + "," + errorMsg;
        this.data = null;
    }

    public void bizError(String errorMsg){
        this.code = SwuferConstant.RESPONSE_BIZ_ERROR_CODE;
        this.msg = "trace-id:" + RpcContext.getContext().getParameter(SwuferConstant.TRACE_ID) + "," + errorMsg;
        this.data = null;
    }

    public void error(String errorMsg,Throwable error){
        this.code = SwuferConstant.RESPONSE_ERROR_CODE;
        this.msg = "trace-id:" + RpcContext.getContext().getParameter(SwuferConstant.TRACE_ID) + "," +errorMsg;
        this.data = null;
        this.error = error;
    }

    public void success(Object data){
        this.code = SwuferConstant.RESPONSE_SUCCESS_CODE;
        this.msg = "success";
        this.data = data;
    }

    public static RpcResponse newError(String errorMsg){
        RpcResponse rpcResponse = new RpcResponse();
        rpcResponse.error(errorMsg);
        return rpcResponse;
    }

    public static RpcResponse newError(String errorMsg,Throwable e){
        RpcResponse rpcResponse = new RpcResponse();
        rpcResponse.error(errorMsg,e);
        return rpcResponse;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Throwable getError() {
        return error;
    }

    public void setError(Throwable error) {
        this.error = error;
    }

    @Override
    public String toString() {
        return "RpcResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
